package com.jn.photogallery.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.jn.photogallery.common.WeakRrfHandler;
import com.jn.photogallery.contract.AvatarContact;
import com.jn.photogallery.entity.DayAvatar;
import com.jn.photogallery.service.AvatarService;
import com.jn.photogallery.utils.LogUtils;


import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class AvatarPresenter implements AvatarContact.AvatarPresenter {
    public static final int HTTP_SUCCESS = 1;
    public static final int HTTP_FAIL = 0;
    private AvatarContact.AvatarView mMainView;
    private Context mContext;

    public AvatarPresenter(AvatarContact.AvatarView mMainView, Context mContext) {
        this.mMainView = mMainView;
        this.mContext = mContext;
    }

    @Override
    public void getData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("https://api.oioweb.cn/api/")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                AvatarService service = retrofit.create(AvatarService.class);
                service.getData().enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        try {
                            String body = response.body().string();
                            String url = call.request().url().toString();
                            LogUtils.d("asdf", "url : " + url);
                            LogUtils.d("asdf", "onResponse : " + body);
                            Message message = Message.obtain();
                            message.obj = new String[]{body, url};
                            message.what = HTTP_SUCCESS;
                            mHandler.sendMessage(message);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                        LogUtils.d("dayImage Failed", "ERROR IN GET DATA");
                    }
                });
            }
        }).start();
    }

    WeakRrfHandler<AvatarPresenter> mHandler = new WeakRrfHandler<>(Looper.getMainLooper(), this, new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            int result = msg.what;
            switch (result) {
                case HTTP_SUCCESS:
                    String[] data = (String[]) msg.obj;
                    if (data == null || data.length < 2) {
                        return false;
                    }
                    String jsonStr = data[0];
                    Gson gson = new Gson();
                    try {
                        DayAvatar dayAvatar = gson.fromJson(jsonStr, DayAvatar.class);
                        if (mMainView != null) {
                            mMainView.showData(dayAvatar);
                        }
                    } catch (JsonSyntaxException e) {
                        LogUtils.d("asdf", "result not json");
                    }
                    break;

                case HTTP_FAIL:
                    break;
            }
            return false;
        }
    });
}
